package net.dgg.zz.api.production.controller;

import io.swagger.annotations.Api;
import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.exception.DggBaseException;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.zz.api.production.dto.ScOrderAndTaskDTO;
import net.dgg.zz.api.production.dto.TaskOperateRecordDTO;
import net.dgg.zz.api.production.service.ApiProductionService;
import net.dgg.zz.common.entity.PageList;
import net.dgg.zz.common.utils.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/api")
@Api(description = "生产给订单中心提供的接口")
public class ApiOrderController extends DggBaseController {

    @Resource
    private ApiProductionService apiProductionService;


    /**
     * 通过生产单id集合查询生产单信息
     * @return
     */
    @PostMapping("/system/portal/task/v1/sc_order_task_by_sc_order_id.do")
    @ResponseBody
    public DggRestResponse scOrderTaskByScOrderId(@RequestBody Map map){
        if(StringUtils.isEmpty(map) || map.size()<1){
            return buildResponse(6002,"参数不正确",map.toString());
        }
        List<Long> scOrderIds = (List<Long>) map.get("scOrderIds");
        Integer start = (Integer) map.get("start");
        Integer limit = (Integer) map.get("limit");
        //参数判断
        if(StringUtils.isEmpty(start)){
            return buildResponse(6002,"参数不正确",start);
        }
        if(StringUtils.isEmpty(limit)){
            return buildResponse(6002,"参数不正确",limit);
        }
        if(StringUtils.isEmpty(scOrderIds) || scOrderIds.size()<1){
            return buildResponse(6002,"参数不正确",scOrderIds);
        }
        try {
            //查询生产单相关信息
            List<ScOrderAndTaskDTO> scOrderAndTaskDTOList = apiProductionService.getScOrderAndTask(scOrderIds, start, limit);
            return buildResponse(200,"操作成功",scOrderAndTaskDTOList);
        } catch (DggBaseException e) {
            logger.error("订单中心调用查询生产信息失败，原因：", e);
            return buildResponse(6001,"系统异常",e);
        } catch (Exception e) {
            logger.error("订单中心调用查询生产信息失败，原因：", e);
            return buildResponse(6001,"系统异常",e);
        }
    }

    /**
     * 通过任务ID查询任务操作记录
     * @return
     */
    @PostMapping("/white/caserecode/v1/query_with_case_records.do")
    @ResponseBody
    public DggRestResponse queryWithCaseRecords(@RequestBody Map map){
        if(StringUtils.isEmpty(map) || map.size()<1){
            return buildResponse(6002,"参数不正确",map.toString());
        }
        String taskId = map.get("taskId").toString();
        String scOrderId = map.get("scOrderId").toString();
        Integer start = (Integer) map.get("start");
        Integer limit = (Integer) map.get("limit");
        //参数判断
        if(StringUtils.isEmpty(start)){
            return buildResponse(6002,"参数不正确",start);
        }
        if(StringUtils.isEmpty(limit)){
            return buildResponse(6002,"参数不正确",limit);
        }
        if(StringUtils.isEmpty(taskId)){
            return buildResponse(6002,"参数不正确",taskId);
        }
        try {
            //查询节点记录
            PageList<TaskOperateRecordDTO> taskOperateRecordDTOs = apiProductionService.getNodeRecords(taskId, scOrderId, start, limit);
            return buildResponse(200,"操作成功",taskOperateRecordDTOs);
        } catch (DggBaseException e) {
            logger.error("订单中心调用查询节点记录信息失败，原因：", e);
            return buildResponse(6001,"系统异常",e);
        } catch (Exception e) {
            logger.error("订单中心调用查询节点记录信息失败，原因：", e);
            return buildResponse(6001,"系统异常",e);
        }
    }

}
